package cn.cloud.all.security.web.savedrequest;

import org.springframework.util.Assert;

import javax.servlet.http.Cookie;
import java.util.*;

public class SimpleSavedRequest implements SavedRequest {

    private String redirectUrl;

    private List<Cookie> cookies = new ArrayList<>();

    private String method = "GET";

    private Map<String, List<String>> headers = new HashMap<>();

    private List<Locale> locales = new ArrayList<>();

    private Map<String, String[]> parameters = new HashMap<>();

    public SimpleSavedRequest() {
    }

    public SimpleSavedRequest(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    public SimpleSavedRequest(SavedRequest request) {
        this.redirectUrl = request.getRedirectUrl();
        this.cookies = request.getCookies();
        for (String headerName : request.getHeaderNames()) {
            this.headers.put(headerName, request.getHeaderValues(headerName));
        }
        this.locales = request.getLocales();
        this.parameters = request.getParameterMap();
    }

    @Override
    public String getRedirectUrl() {
        return this.redirectUrl;
    }

    @Override
    public List<Cookie> getCookies() {
        return this.cookies;
    }

    @Override
    public String getMethod() {
        return null;
    }

    @Override
    public List<String> getHeaderValues(String name) {
        return this.headers.getOrDefault(name, new ArrayList<>());
    }

    @Override
    public Collection<String> getHeaderNames() {
        return this.headers.keySet();
    }

    @Override
    public List<Locale> getLocales() {
        return this.locales;
    }

    @Override
    public String[] getParameterValues(String name) {
        return this.parameters.getOrDefault(name, new String[0]);
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return this.parameters;
    }

    public void setRedirectUrl(String redirectUrl) {
        Assert.notNull(redirectUrl, "redirectUrl cannot be null");
        this.redirectUrl = redirectUrl;
    }

    public void setCookies(List<Cookie> cookies) {
        Assert.notNull(cookies, "cookies cannot be null");
        this.cookies = cookies;
    }

    public void setMethod(String method) {
        Assert.notNull(method, "method cannot be null");
        this.method = method;
    }

    public void setHeaders(Map<String, List<String>> headers) {
        Assert.notNull(headers, "headers cannot be null");
        this.headers = headers;
    }

    public void setLocales(List<Locale> locales) {
        this.locales = locales;
    }

    public void setParameters(Map<String, String[]> parameters) {
        Assert.notNull(parameters, "parameters cannot be null");
        this.parameters = parameters;
    }
}
